#include "data.h"

schedule::schedule( data &md){

		nbSched = md.getnbJobs();
		nbStages = md.getnbStages();
		nbMachinesPerStage = md.getnbMachinesPerStage();
		int i,j;
		for ( i = 0; i < nbSched; i++) 
			sched.push_back( i );		

		for ( i = 0; i < nbStages; i++) {
			stages.push_back(i);
			for ( j = 0; j < nbMachinesPerStage; j++)
				machines.push_back( i * nbMachinesPerStage + j );
		}

		for ( i = 0; i <nbSched; i ++) 
			for ( j = 0; j < nbStages; j++) {
				ptimes.push_back( md.getPtimes(i,j));
				rtimes.push_back( i * nbStages + j);
				stimes.push_back( i * nbStages + j);
				etimes.push_back( i * nbStages + j);

			}

	}



int schedule::getnbSched() {
		return nbSched;
	}
	
int schedule::getnbStages() {
		return nbStages;
	}

int schedule::getSched(int i) {
		return sched[i];
	}

void schedule::setSched( int i, int j) {
		sched[i] = j;
	}

void schedule::deleteSched(int i) {
		deque<int>::iterator it = sched.begin();
		sched.erase(it +i );
		nbSched --;

    	it = ptimes.begin();
		ptimes.erase( it + i * nbStages, it + (i + 1) * nbStages );
	}

void schedule::insertBeforSched(int i, int j) {
		deque<int>::iterator it = sched.begin();
		sched.insert(it +i - 1,j);
	}


void schedule::insertAfterSched(int i, int j) {
		deque<int>::iterator it = sched.begin();
		sched.insert(it +i,j);
	}
	
void schedule::setPtimes( int i, int j, int k) {	
		ptimes[i * nbStages +j ] = k;
	}

int schedule::getPtimes( int i, int j) {	
		return ptimes[i * nbStages +j ];
	}


void schedule::setRtimes( int i, int j, int k) {	
		rtimes[i * nbStages +j ] = k;
	}

int schedule::getRtimes( int i, int j) {	
		return rtimes[i * nbStages +j ];
	}

void schedule::setStimes( int i, int j, int k) {	
		stimes[i * nbStages +j ] = k;
	}

int schedule::getStimes( int i, int j) {	
		return stimes[i * nbStages +j ];
	}

void schedule::setEtimes( int i, int j, int k) {
		etimes[i * nbStages +j ] = k;
	}

int schedule::getEtimes( int i, int j) {	
	return etimes[i * nbStages +j ];
	}


int schedule::getMakespan() {	
	int i;
	makespan = 0; // made error here. just like RAND_MAX
	for ( i = 0; i < nbSched; i++){
		if ( makespan < etimes[ i * nbStages + nbStages - 1 ] ) 
			makespan = etimes[ i * nbStages  + nbStages - 1 ];
	
	}	
	return makespan;
}


schedule::getMaxWait() {
	int i, j, wait, maxWait = 0;
	for (i = 0; i <nbSched; i ++  ) {

		for ( j = 0; j < nbStages - 1 ; j ++) {

			wait = stimes [ i * nbStages + j + 1]
				- etimes[i * nbStages + j];
			if (maxWait < wait)
				maxWait = wait;
		}
	}
	return maxWait;
}


void schedule::setMakespan( int i) {
	makespan = i;
}

void schedule::setMachines( int i, int j, int k ) {
		machines[ i * nbStages +j ] = k;
	}

int schedule::getMachines( int i, int j) {
		return machines [i * nbStages +j];
	}

int schedule::getnbMachinesPerStage() {
		return nbMachinesPerStage;
	}

int schedule::getLast() {
		return sched[nbSched - 1];
	}